package com.luhuiguo.cms.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.client.GlobalResources;
import com.luhuiguo.cms.client.presenter.ArticleProxy;
import com.luhuiguo.cms.client.presenter.ArticlesPresenter;
import com.luhuiguo.cms.shared.model.ArticleListModel;
import com.luhuiguo.cms.shared.model.ArticleModel;

@Singleton
public class ArticlesView extends ContentPanel implements ArticlesPresenter.Display {

    private GlobalConstants constants;

    private GlobalMessages messages;

    private GlobalResources resources;

    private ArticleProxy proxy;

    private PagingLoader<PagingLoadResult<ModelData>> loader;

    private ListStore<BeanModel> store;

    private Grid<BeanModel> grid;

    private Button addButton;

    private Button deleteButton;

    private Button clearCacheButton;

    @Inject
    public ArticlesView(GlobalConstants constants, GlobalMessages messages, GlobalResources resources,
            ArticleProxy proxy) {
        this.constants = constants;
        this.messages = messages;
        this.resources = resources;
        this.proxy = proxy;
        init();

    }

    private void init() {

        loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy, new BeanModelReader());
        // loader.setRemoteSort(true);

        store = new ListStore<BeanModel>(loader);

        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();

        ColumnConfig col = sm.getColumn();
        columns.add(col);

        col = new ColumnConfig("id", messages.id(), 50);
        columns.add(col);

        col = new ColumnConfig("title", messages.title(), 200);
        col.setRenderer(new GridCellRenderer<BeanModel>() {
            @Override
            public Object render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex,
                    ListStore<BeanModel> store, Grid<BeanModel> grid) {
                ArticleListModel a = model.getBean();
                String ret = "";
                ret = ret + " <a href='/a-" + a.getId() + ".html' target='_blank'>" + a.getTitle() + "</a>";
                return ret;
            }
        });
        columns.add(col);

        col = new ColumnConfig("author", messages.author(), 80);
        columns.add(col);

        col = new ColumnConfig("source", messages.source(), 80);
        columns.add(col);

        col = new ColumnConfig("rank", messages.rank(), 80);
        columns.add(col);

        col = new ColumnConfig("lastModified", messages.lastModified(), 120);
        final DateTimeFormat dtformat = DateTimeFormat.getFormat(constants.defaultDateTimeFormat());

        col.setRenderer(new GridCellRenderer<BeanModel>() {

            @Override
            public Object render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex,
                    ListStore<BeanModel> store, Grid<BeanModel> grid) {
                ArticleListModel a = model.getBean();

                return dtformat.format(a.getLastModified());
            }
        });
        columns.add(col);

        col = new ColumnConfig("lastModifiedBy", messages.lastModifiedBy(), 80);
        columns.add(col);

        col = new ColumnConfig("status", messages.status(), 80);
        col.setRenderer(new GridCellRenderer<BeanModel>() {

            @Override
            public Object render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex,
                    ListStore<BeanModel> store, Grid<BeanModel> grid) {
                ArticleListModel a = model.getBean();
                String ret = "";

                int status = a.getStatus();

                switch (status) {
                case ArticleModel.DRAFT:
                    ret = messages.draft();
                    break;
                case ArticleModel.PENDING:
                    ret = messages.pending();
                    break;
                case ArticleModel.APPROVED:
                    ret = messages.approved();
                    break;
                default:
                    ret = messages.abandoned();
                    break;
                }

                return ret;
            }
        });
        columns.add(col);

        ColumnModel cm = new ColumnModel(columns);

        grid = new Grid<BeanModel>(store, cm);
        grid.setSelectionModel(sm);
        grid.setStripeRows(false);
        grid.addPlugin(sm);
        grid.setAutoExpandColumn("title");
        grid.setLoadMask(true);
        // grid.setStateful(true);
        // grid.setStateId("articles");

        ToolBar toolBar = new ToolBar();

        addButton = new Button(messages.newArticle(), AbstractImagePrototype.create(resources.addIcon()));
        addButton.setEnabled(false);
        deleteButton = new Button(messages.deleteArticle(), AbstractImagePrototype.create(resources.deleteIcon()));
        deleteButton.setEnabled(false);

        clearCacheButton = new Button(messages.clearCache(), AbstractImagePrototype.create(resources.clearIcon()));

        toolBar.add(addButton);
        toolBar.add(new SeparatorToolItem());
        toolBar.add(deleteButton);
        toolBar.add(new SeparatorToolItem());
        toolBar.add(clearCacheButton);

        setTopComponent(toolBar);

        PagingToolBar pagingBar = new PagingToolBar(constants.defaultPageSize());
        pagingBar.bind(loader);
        setBottomComponent(pagingBar);

        setHeading(messages.articles());
        setLayout(new FitLayout());
        add(grid);
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void reloadData() {
        loader.load();

    }

    @Override
    public Button getAddButton() {
        return addButton;
    }

    @Override
    public Button getDeleteButton() {
        return deleteButton;
    }

    @Override
    public Button getClearCacheButton() {
        return clearCacheButton;
    }

    @Override
    public Grid<BeanModel> getGrid() {
        return grid;
    }

}
